<!--
 * @Author: ckk
 * @Date: 2022-09-01 13:39:27
 * @LastEditors: ckk
 * @LastEditTime: 2022-09-08 15:53:08
 * @Description: 
-->
<template>
  <div class="addidentification">
    <el-dialog :title="title" :visible.sync="dialogFormVisible" center>
      <el-form :model="addformdata" :rules="rules" ref="typeFrom" label-width="80px">
        <el-form-item label="标识名称">
          <el-input v-model="addformdata.name" autocomplete="off" maxlength="15" show-word-limit></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="addformdata.note" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveIdentification">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { addAppType } from "@/api/basisMG";
export default {
  props: {
    adDialogFormVisible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      identtype: "",
      dialogFormVisible: false,
      title: "",
      addformdata: {
        name: "",
        note: "",
        productType: "",
        type: 3,
        id: "",
        dataUrl: "",
        pageUrl: "",
      },
      rules: {
        name: [{ required: true, message: "请填写标识名称", trigger: "blur" }],
      },
    };
  },
  methods: {
    showEdit(row) {
      if (row.isSet == 0) {
        this.dialogFormVisible = true;
        this.title = "添加标识";
        this.addformdata.name = "";
        this.addformdata.note = "";
        this.addformdata.id = "";
      } else {
        this.dialogFormVisible = true;
        this.title = "编辑标识";
        this.addformdata.name = row.name;
        this.addformdata.note = row.note;
        this.addformdata.id = row.id;
      }
    },
    saveIdentification() {
      this.addformdata.name = this.addformdata.name.replace(
        /[^\a-\z\A-\Z0-9\u4e00-\u9fe5_]/g,
        ""
      );
      this.addformdata.note = this.addformdata.note.replace(
        /[^\a-\z\A-\Z0-9\u4e00-\u9fe5_]/g,
        ""
      );
      this.$refs["typeFrom"].validate(async (valid) => {
        if (valid) {
          const data = await addAppType(this.addformdata);
          if (data.code != 200) return this.$message.error(data.message);
          this.$message.success(data.message);
          setTimeout(() => {
            this.dialogFormVisible = false;
            this.emitList();
          }, 500);
        } else {
          this.$message.warning("提交失败, 请检查内容是否填写正确!!!");
          return false;
        }
      });
    },
    emitList(e) {
      this.$emit("editorList", e);
    },
  },
  watch: {
    // 监听 adDialogFormVisible 改变
    adDialogFormVisible(oldVal, newVal) {
      this.dialogFormVisible = this.adDialogFormVisible;
    },
  },
};
</script>
<style lang="scss"></style>
